import { GET, POST,POSTJSON, PUT, DELETE,POSTFORM } from './index'
// 微信登录
const wxLoginManager= () => {
    return new Promise (resolve => {
        let that = this
        uni.login({
            provider:'weixin',
            success: function (loginRes) {
							console.log('loginRes', loginRes)
							if (loginRes.code){
								var params = { 'code':loginRes.code }
								getOpenId(params).then(res => {
									resolve(res)
								})
							}
            }
        })
    })
}


// 根据code 换 openid
const getOpenId=params=>{
	const url = '/api/login/getOpenId'
	var params = {
		code:params.code
	}
	return new Promise (resolve => {
		POSTJSON(url,params).then(res=>{
			console.log(res)
			if (res.success === true){
				// 1. 持久保存用户信息
				uni.setStorageSync('openid',res.data.openid);
				uni.setStorageSync('sessionKey',res.data.session_key);
				// 1. 存入token
				resolve(res)
			}
		})
	})
}
module.exports = {
	wxLoginManager}